<?php
/**
 * Url助手类
 *
 * @author        chenfenghua <843958575@qq.com>
 * @copyright     Copyright (c) 2015-2016 Yiishop. All rights reserved.
 * @link          http://www.yiishop.com.cn
 * @package       BageCMS.Tools
 * @license       http://www.yiishop.com.cn/license
 * @version       v2.0.0
 */

namespace app\models\Helper;

use app\models\member\MemberOther;
use Yii;
use yii\helpers\ArrayHelper;

class Url
{

    /**
     * 获取用户头像
     *
     * @param string $avatar
     * @return string
     */
    public static function img($image)
    {
        $params = \Yii::$app->params;
        $imgDomainUrl = $params['img_host'];
        if ($image) {
            $http = substr($image, 0, 4);
            if($http == 'http'){
                return $image;
            }else{
                return $imgDomainUrl . $image;
            }
        } else {
            return $imgDomainUrl . '/images/default.jpg';
        }
    }

    /**
     * 判断用户绑定第三方信息
     * @param $user_id
     * @return mixed
     */
    public static function isBindOther($user_id)
    {
        //绑定第三方信息
        $others = MemberOther::findAll(['user_id' => $user_id]);
        $others_type = ArrayHelper::getColumn($others, 'type');
        //var_dump($others_type);exit;

        if (in_array(2, $others_type)) {
            $data['is_bind_sina'] = '1';
        } else {
            $data['is_bind_sina'] = '0';
        }

        if (in_array(1, $others_type)) {
            $data['is_bind_weixin'] = '1';
        } else {
            $data['is_bind_weixin'] = '0';
        }

        if (in_array(0, $others_type)) {
            $data['is_bind_qq'] = '1';
        } else {
            $data['is_bind_qq'] = '0';
        }

        return $data;
    }




    /**
     * 生成url
     *
     * @param string $route
     * @param array $params
     * @return string
     */
    public static function to($route = '', $params = [])
    {
        $str = '';
        if ($params) {
            $arr = [];
            foreach ($params as $k=>$v) {
                $arr[] = $k.'='.$v;
            }
            $str = implode('&', $arr);
        }
        return Yii::$app->params['web_url'].'/'.($route?$route.'.html':'').($str?'?'.$str:'');
    }

    public static function to2($route = '', $params = [])
    {
        $str = '';
        if ($params) {
            $arr = [];
            foreach ($params as $k=>$v) {
                $arr[] = $k.'='.$v;
            }
            $str = implode('&', $arr);
        }
        return Yii::$app->params['wap_url'].'/'.($route?$route.'.html':'').($str?'?'.$str:'');
    }
}